// pages/classifyCommodity/classifyCommodity.js
var {getHomeGoods}=require("../../api/Home")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsList:[],//商品列表
        listId:null,//列表id
    },
//获取相对应的商品列表的数据
getHomeGoodsFun(listId){
    getHomeGoods({cid:listId,pagesize:30}).then(data=>{
        let lists = [];
        let goodslists = data.message.goods;
        goodslists.forEach(item => {
            let {
                goods_id,
                goods_small_logo,
                goods_name,
                goods_price
            } = item;
            let data = {
                id: goods_id,
                goodsName: goods_name,
                goodsPrice: goods_price,
                goodsImage: goods_small_logo,
                originalPrice: goods_price + 100,
            };
            if (goods_small_logo) {
                lists.push(data);
            }
        })
        this.setData({
            goodsList: lists
        })
    })
},
// 添加到购物车
addShopping(options) {
    let id = options.target.dataset.id;
    let index = this.data.goodsList.findIndex(item => item.id == id);
    // console.log(index);
    let cartLists = {
        id: this.data.goodsList[index].id,
        goodsImage: this.data.goodsList[index].goodsImage,
        goodsName: this.data.goodsList[index].goodsName,
        goodsPrice: this.data.goodsList[index].goodsPrice,
        num: 1,
    };
    let goodsCarList = wx.getStorageSync('goodsCarList');
    if (goodsCarList) {
        let i = goodsCarList.findIndex(item => item.id == id);
        if (i != -1) {
            goodsCarList[i].num += 1;
        } else {
            goodsCarList.push(cartLists);
        }
        wx.setStorageSync('goodsCarList', goodsCarList);
    } else {
        wx.setStorageSync('goodsCarList', [cartLists])
    }
},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
this.setData({
    listId:options.id
})
this.getHomeGoodsFun(this.data.listId)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})